package studentsinfomanagement.view.administrator;

import studentsinfomanagement.bean.Student;
import studentsinfomanagement.dao.DepartmentDAO;
import studentsinfomanagement.dao.StudentDAO;

import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.List;
/**
 * 管理员管理学生信息界面类

 */
public class AdministratorStudentCenter extends JFrame {

    private AdministratorStudentCenter administratorStudentCenter = null;
    private StudentDAO studentDAO = null;//学生数据层管理类
    private DepartmentDAO departmentDAO = null;
    private DefaultTableModel tableModel = null;
    private JTable table = null;
    private JScrollPane scrollPane = null;
    private String[] columnNames = null;  // 列名
    private String [][]tableVales= null; //数据

    {
        table = new JTable();
    }

    public AdministratorStudentCenter(){
        administratorStudentCenter = this;
        studentDAO = StudentDAO.getInstance();
        departmentDAO = DepartmentDAO.getInstance();

        this.setTitle("管理学生信息");//标题
        this.setSize(550,500);//大小
        this.setLocationRelativeTo(null);//居中
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);//关闭操作隐藏界面
        this.setResizable(false);//大小不可调节

        BorderLayout bl = new BorderLayout();
        this.setLayout(bl);

        //界面元素
        //课程滚动显示区域
        showAllStudents();//获取全部授课信息
        scrollPane = new JScrollPane(table);

        //退出选课中心
        JPanel jp_exit = new JPanel();
        JButton btn_exit = new JButton("退出");
        jp_exit.add(btn_exit);

        //将组件添加到界面
        this.add(scrollPane,BorderLayout.CENTER);
        this.add(jp_exit,BorderLayout.SOUTH);

        //界面可见
        this.setVisible(true);

        //添加监听器
        //行选择监听器
        table.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
            @Override
            public void valueChanged(ListSelectionEvent e) {
                //获取选择的行的学生学号
                int sel_row = table.getSelectedRow();
                if (sel_row >= 0) {//过滤掉不符合逻辑的值
                    int sel_column = table.getSelectedColumn();
                    String sno = (String) table.getValueAt(sel_row, 0);
                    Student student = new Student();
                    student.setSno(sno);
                    if (sel_column == 4) {//选择删除学生
                        boolean result = studentDAO.delete(student);
                        if (!result) {
                            JOptionPane.showMessageDialog(administratorStudentCenter, "删除学生失败！");
                        } else {
                            JOptionPane.showMessageDialog(administratorStudentCenter, "删除学生成功！");
                            //更新界面数据
                            showAllStudents();
                        }
                    } else {//显示该学生的选课信息界面
                        new SCAdministratorView(student);
                    }
                }
            }
        });

        //退出
        btn_exit.addMouseListener(new MouseListener() {
            @Override
            public void mouseClicked(MouseEvent e) {
                administratorStudentCenter.setVisible(false);
            }

            @Override
            public void mousePressed(MouseEvent e) {

            }

            @Override
            public void mouseReleased(MouseEvent e) {

            }

            @Override
            public void mouseEntered(MouseEvent e) {

            }

            @Override
            public void mouseExited(MouseEvent e) {

            }
        });
    }

    /**
     * 获取所有学生信息
     */
    public void showAllStudents(){

        List<Student> studentList = studentDAO.getAllStudents();
        columnNames = new String[]{"学号","姓名","性别","专业","操作"};
        tableVales = new String[studentList.size()][];
        int i = 0;
        for(Student s:studentList){
            String sno = s.getSno();
            String name = s.getName();
            String sex = s.getSex();
            String dno = s.getDepartment();
            String department = departmentDAO.getDepartmentByDno(dno).getName();
            tableVales[i] = new String[5];
            tableVales[i][0] = sno;
            tableVales[i][1] = name;
            tableVales[i][2] = sex;
            tableVales[i][3] = department;
            tableVales[i][4] = "删除";
            i++;
        }
        tableModel = new DefaultTableModel(tableVales,columnNames){
            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };
        table.setModel(tableModel);
        revalidate();
    }

}
